package com.dawn.iiotgateway.api;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dawn.iiotgateway.daemon.OpcUAService;
import com.dawn.iiotgateway.entity.OpcConfig;
import com.dawn.iiotgateway.entity.UserInfo;
import com.dawn.iiotgateway.entity.Variable;
import com.dawn.iiotgateway.result.Result;
import com.dawn.iiotgateway.service.IOpcConfigService;
import com.dawn.iiotgateway.service.IUserInfo;
import com.dawn.iiotgateway.service.IVariableService;
import com.dawn.iiotgateway.vo.UserInfoQueryVo;
import com.dawn.iiotgateway.vo.LoginVo;
import com.dawn.iiotgateway.vo.SensorDataVo;
import com.dawn.iiotgateway.vo.SystemUserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
public class OpcConfigController {

    @Autowired
    private IOpcConfigService opcConfigService;

    @Autowired
    private IVariableService variableService;

    @Autowired
    private OpcUAService opcUAService;

    @PostMapping("/admin/login")
    public Result<String> login(@RequestBody LoginVo loginVo) {

        return Result.ok("total_nitrogen_out");
    }

    @GetMapping("/admin/info")
    public Result<SystemUserInfoVo> info() {
        System.out.println(Thread.currentThread().getName());
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName("李帅辉");
        systemUserInfoVo.setAvatarUrl("/assets/logo-af7956e2.png");

        return Result.ok(systemUserInfoVo);
    }

    @GetMapping("/admin/configurations")
    public String configurations() {

        List<OpcConfig> configurations = opcConfigService.getConfigurations();
        return JSONObject.toJSONString(configurations);
    }

    @GetMapping("/admin/variables")
    public Result<List<Variable>> getVariables() {

        List<Variable> variables = variableService.list();
        Result<List<Variable>> ok = Result.ok(variables);
        return ok;
    }

    @GetMapping("/admin/getSensordata")
    public Result<List<Variable>> getOpcSensorData() {

        System.out.println(Thread.currentThread().getName());
        //List<SensorDataVo> list = new ArrayList<>();
//        for (String key : messages.keySet()) {
//
//            if(!key.equals("timestamp") && !key.equals("station")) {
//                String data = (String)messages.get(key);
//                list.add(new SensorDataVo(key, timestamp, data));
//            }
//
//        }
//        String timestamp = (String)messages.get("timestamp");


        List<Variable> variables = opcUAService.getVariables();


        return Result.ok(variables);
    }

    @Autowired
    private IUserInfo service;

    @GetMapping("/admin/system/user/page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVo queryVo) {

        Page<UserInfo> page = new Page<>(current, size);

        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryVo.getPhone() != null, UserInfo::getPhone, queryVo.getPhone());
        queryWrapper.eq(queryVo.getStatus() != null, UserInfo::getStatus, queryVo.getStatus());

        Page<UserInfo> result = service.page(page, queryWrapper);

        return Result.ok(result);
    }

}
